#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

from PyQt5.QtWidgets import QGroupBox, QWidget
from PyQt5.QtWidgets import QVBoxLayout

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

from matplotlib.figure import Figure


from PyQt5 import QtCore

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


class CurvaturePressureGraph(QWidget):
    """This is the element that contains the cam
    profile
    """
    def __init__(self, parent):
        super(CurvaturePressureGraph, self).__init__(parent)
        
        # Aesthetics
        #self.setFrameShape(self.StyledPanel)
        #self.setFrameShadow(self.Raised)

        # Group Box
        self.groupbox = QGroupBox(self)
        #self.groupbox.setTitle(_fromUtf8("Cam profile"))

        # The layout
        self.mainLayout = QVBoxLayout(self)
        self.mainLayout.addWidget(self.groupbox)
        # Group Box layout
        self.gboxVBox = QVBoxLayout(self.groupbox)

        # Create figure container
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.gboxVBox.addWidget(self.canvas)

        # Toolbar
        self.toolBarCamProfile = NavigationToolbar(self.canvas, self.groupbox)
        self.gboxVBox.addWidget(self.toolBarCamProfile)

        # Initialize graph
        self.InitDefaultGraph()

    def InitDefaultGraph(self):
        """Creates the default layout
        """
        self.curvatureRadiusGraph = self.figure.add_subplot(211)
        self.pressureAngleGraph = self.figure.add_subplot(212)

        self.pressureAngleGraph.grid(True)
        self.curvatureRadiusGraph.grid(True)
        self._initCurvatureRadiusLabels()
        self._initPressureAngleLabels()

        self.figure.tight_layout()

    def _initCurvatureRadiusLabels(self):
        """This initializes the labels and the titles
        
        """
        # Curvature radius
        self.curvatureRadiusGraph.set_title(_fromUtf8("Curvature radius"))
        self.curvatureRadiusGraph.set_xlabel(_fromUtf8("Angle [º]"))
        self.curvatureRadiusGraph.set_ylabel(_fromUtf8("Rc [mm]"))

    def _initPressureAngleLabels(self):
        """Initializes labels and titles
        """
        # Pressure Angle
        self.pressureAngleGraph.set_title(_fromUtf8("Pressure angle"))
        self.pressureAngleGraph.set_xlabel(_fromUtf8("Angle [º]"))
        self.pressureAngleGraph.set_ylabel(_fromUtf8("ϕ [º]"))


    def genPressureAnglePlot(self, abcissae, ordinate):
        self.pressureAngleGraph.clear()
        self._initPressureAngleLabels()
        self.pressureAngleGraph.plot(abcissae, ordinate, '-')
        self.pressureAngleGraph.grid(True)
        self.canvas.draw()

    def genCurvatureRadiusPlot(self, abcissae, ordinate):
        self.curvatureRadiusGraph.clear()
        self._initCurvatureRadiusLabels()
        self.curvatureRadiusGraph.plot(abcissae, ordinate, '-')
        self.curvatureRadiusGraph.grid(True)
        self.canvas.draw()

    def genPressureAnglePlotBi(self, abcissae, ordinate1, ordinate2):
        self.pressureAngleGraph.clear()
        self._initPressureAngleLabels()
        self.pressureAngleGraph.plot(abcissae, ordinate1, 'b-')
        self.pressureAngleGraph.plot(abcissae, ordinate2, 'g-')
        self.pressureAngleGraph.grid(True)

    def genCurvatureRadiusPlotBi(self, abcissae, ordinate1, ordinate2):
        self.curvatureRadiusGraph.clear()
        self._initCurvatureRadiusLabels()
        self.curvatureRadiusGraph.plot(abcissae, ordinate1, 'b-')
        self.curvatureRadiusGraph.plot(abcissae, ordinate2, 'g-')
        self.curvatureRadiusGraph.grid(True)
